# include <iostream>
# include <thread>
# include <chrono>  // 时间相关
# include <functional>  // 函数分类
# include "cpp-httplib/httplib.h"  //网络下载


class Download{
    private:
    /*data*/
    public:
        void download(const std::string &host,const std::string &path,const std::function<void(const std::string &,const std::string &)> &callback){
            std::cout << "线程" << std::this_thread::get_id() << std::endl;  // 输出线程ID
            httplib::Client cli(host);
            auto response = cli.Get(path);
            if(response && response->status == 200){
                callback(path,response->body);
            }
            else{
                std::cout << "下载失败" << std::endl;
            }

        };

        void start_download(const std::string &host,const std::string &path,const std::function<void(const std::string &,const std::string &)> &callback){
            // auto download = std::bind(&Download::download,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
            // std::thread thread(download,host,path,callback);  // 创建线程
            std::thread thread([this,host,path,callback](){
                this->download(host,path,callback);
            });   // 使用匿名函数创建线程更好理解逻辑
            thread.detach();  // 线程分离
      };
};

int main(){
    auto download = Download();
    auto callback = [](const std::string &path,const std::string &result){
        std::cout << "下载完成" << path << result.length() << result.substr(0,10) << std::endl;  /// 输出下载结果 ,只输出前10个字符
    };
    std::cout <<"萨健康的科技阿斯顿撒" << std::endl;
    download.start_download("http://0.0.0.0:8000","/novel1.txt",callback);
    download.start_download("http://0.0.0.0:8000","/nove12.txt",callback);
    download.start_download("http://0.0.0.0:8000","/nove13.txt",callback);
    std ::this_thread::sleep_for(std::chrono::seconds(1));

    return 0;

}